using System;
using System.Collections;
using Gtk;

using utilities;

public partial class MainWindow: Gtk.Window
{	
	//private AlarmTimer alarm = new AlarmTimer ();

	private Gtk.NodeStore store = new Gtk.NodeStore(typeof (AlarmTimer));
	private Gtk.NodeStore Store {
		get {
			return store;
		}
	}
	
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
		
		GLib.Timeout.Add(1000, new GLib.TimeoutHandler(update_status));
		//show_status();
		
		viewAlarm.NodeStore = Store;
		viewAlarm.AppendColumn("Start Time", new Gtk.CellRendererText(), "text", 0);
		viewAlarm.AppendColumn("Alarm Time", new Gtk.CellRendererText(), "text", 1);
		viewAlarm.AppendColumn("Duration", new Gtk.CellRendererText(), "text", 2);
		viewAlarm.AppendColumn("Remain", new Gtk.CellRendererText(), "text", 3);
		viewAlarm.AppendColumn("Set", new Gtk.CellRendererText(), "text", 4);
		viewAlarm.AppendColumn("Count", new Gtk.CellRendererText(), "text", 5);
		viewAlarm.AppendColumn("Comment", new Gtk.CellRendererText(), "text", 6);
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}
	
	private bool update_status()
	{
		foreach (AlarmTimer alarm in store) {
			if (alarm.IsAlarmSet) {
				alarm.decreaseRemain();
			
				if (alarm.alarmReached ()) { 
					
					alarm.alarmPlaySound();
					this.Show();
					this.Present();				
	
					alarm.setNextAlarm();
				}			
			}
		}
		viewAlarm.QueueDraw();
		return true;
	}
	
	protected void onStartClicked (object sender, System.EventArgs e)
	{
		AlarmTimer timer = new AlarmTimer(getDuration(), txtComment.Text);
		store.AddNode(timer);
	}
	
	private TimeSpan getDuration() 
	{
		string[] tokens = txtDuration.Text.Split(':');
		switch (tokens.Length) {
		case 1:
			return new TimeSpan(0, Convert.ToInt32(tokens[0]), 0, 0);
		case 2:
			return new TimeSpan(0, Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1]), 0);
		case 3:
			return new TimeSpan(0, Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]));
		case 4:
			return new TimeSpan(Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3]));
		default:
			throw new NotImplementedException ();
		}
	}
	
//	private void show_status()
//	{
//		labCurrentTime.LabelProp = DateTime.Now.ToString();
//		if (alarm.IsAlarmSet) {
//			labCurrentTime.LabelProp += "\nStart    : " + alarm.StartTime.ToString();
//			labCurrentTime.LabelProp += "\nNext     : " + alarm.AlarmTime.ToString();
//			labCurrentTime.LabelProp += "\nDuration : " + alarm.Duration.ToString();
//			labCurrentTime.LabelProp += "\nCount    : " + alarm.AlarmCount.ToString();
//			
//			labCountDown.LabelProp = alarm.Remain.ToString();
//		}
//		else {
//			labCountDown.LabelProp = "";
//		}
//	}

	protected void onToggleClicked (object sender, System.EventArgs e)
	{
		foreach (AlarmTimer node in viewAlarm.NodeSelection.SelectedNodes) {
			if (!node.IsAlarmSet) 
				node.setAlarm(node.Duration);
			else
				node.noAlarm();
		}
		viewAlarm.QueueDraw();
	}

	protected void onDeleteClicked (object sender, System.EventArgs e)
	{
		foreach (AlarmTimer node in viewAlarm.NodeSelection.SelectedNodes) {
			store.RemoveNode(node);
		}
		viewAlarm.QueueDraw();
	}
}
